package pers.richard.ormybatis.bean.mapping;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import pers.richard.ormybatis.bean.AbstractDomain;
import pers.richard.ormybatis.util.CaseFormatUtil;

/**
 * insert批量对象mapping参数bean
 */
public class InsertBatchMappingBean extends BaseMappingBean{
	/** po 对象 map形式 */
	private List<Map<String, Object>> poMapList = new ArrayList<Map<String,Object>>();

	
	public InsertBatchMappingBean(){}
	
	public InsertBatchMappingBean(String tableName, String primaryKeyName, List poList, boolean lowerUnderscore){
		super(tableName, primaryKeyName);
		Date now = new Date();
		for (Object po : poList) {
			AbstractDomain abstractDomainPo = (AbstractDomain)po;
            Map<String, Object> poMap =  lowerUnderscore ? abstractDomainPo.toMapKeyToUnderscore() : abstractDomainPo.toMap();
            Field[] fields = po.getClass().getDeclaredFields();
    		for (Field field : fields) {
    			String fieldName = field.getName();
    			Object value = BeanUtil.getProperty(po, fieldName);
    			if (StrUtil.equalsAnyIgnoreCase(fieldName, CREATE_FIELD_PROPERTY) && ObjectUtil.isEmpty(value) ) {
    				value = now;
    			}
    			if (StrUtil.equalsAnyIgnoreCase(fieldName, UPDATE_FIELD_PROPERTY) && ObjectUtil.isEmpty(value) ) {
    				value = now;
    			}
    			if (StrUtil.equalsAnyIgnoreCase(fieldName, "isDeleted", "updateVersion") && ObjectUtil.isEmpty(value) ) {
    				value = 0;
    			}
    			String key =  lowerUnderscore ? CaseFormatUtil.toLowerUnderscore(field.getName()) : CaseFormatUtil.toLowerCamel(field.getName());
    			poMap.put(key, value);
    		}
            poMapList.add(poMap);
        }
	}

	public List<Map<String, Object>> getPoMapList() {return poMapList;}
	public void setPoMapList(List<Map<String, Object>> poMapList) {this.poMapList = poMapList;}

}
